package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        try {
            // 1. 查询护理计划(基础信息)
            NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
            // 2. 查询护理计划和护理项目的关系信息(中间表)
            List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);
            // 3.组装数据到VO
            NursingPlanVo nursingPlanVo = new NursingPlanVo();
            BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
            nursingPlanVo.setProjectPlans(projectPlans);
            return nursingPlanVo;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        try {
            // 1. 保存护理计划(基础信息)
            NursingPlan nursingPlan = new NursingPlan();
            // 属性的名字和类型需要一致
            BeanUtils.copyProperties(dto, nursingPlan);
            // 设置创建时间
            nursingPlan.setCreateTime(DateUtils.getNowDate());
            nursingPlan.setUpdateTime(DateUtils.getNowDate());
            nursingPlanMapper.insertNursingPlan(nursingPlan);

            // 2.批量保存护理计划和护理项目的关系(存入中间表)
            int row = nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), dto.getProjectPlans());

            return row == 0 ? 0 : 1;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            // 1. 修改护理计划(基础信息)
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);

            // 2.判断DTO中是否存在护理项目的列表
            List<NursingProjectPlanDto> list = dto.getProjectPlans();
            if (list != null && !list.isEmpty()) {
                // 代表存在护理项目的列表 , 先删除 , 再添加
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), list);
            }
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteNursingPlanById(Long id) {
        try {
            nursingProjectPlanMapper.deleteByPlanId(id);
            return nursingPlanMapper.deleteNursingPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
